package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author aili
 * @create 2020-05-16 12:46
 */


@RestController
@RequestMapping("/address")
public class AddressController {
    @Reference
    private AddressService addressService;



    @RequestMapping("/getAllAddress")

        public Result getAllAddress(){
            try {

                List<Address> allAddress = addressService.getAllAddress();
                return new Result(true,"地址信息显示成功",allAddress);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(true,"地址信息显示错误");
            }

        }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {
        //1.调用checkItemService的findPage方法
        PageResult pageResult = addressService.findPage(pageBean);
        System.out.println(pageResult);
        return pageResult;
    }
    //@RequestBody Address address
    @RequestMapping("/add")
    public Result add(@RequestBody Address address) throws IOException {
        try {
            addressService.add(address);
            return new Result(true,"地址添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"地址添加失败");
        }
    }
    @RequestMapping("/delAddress")
    public Result delete(@RequestParam("id") int id){
        try {
            addressService.delete(id);
            return new Result(true,"地址删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"地址删除失败");
        }
    }

}
